package com.yd.controller.system;


import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yd.pojo.Administrator;
import com.yd.pojo.Agency;
import com.yd.service.AdministratorService;
import com.yd.service.AgencyService;
import com.yd.tool.MD5Hash;
import com.yd.tool.UtsReg;

/**
 * 管理员修改代理商信息
 * @author Administrator
 *
 */
@Controller
public class AgencyControllerSys {

	@Resource
	private AgencyService agencyService;
	@Resource
	private AdministratorService administratorService;
	
	//代理商密码修改
	@RequestMapping("/sys/agency/pass/up")
	@ResponseBody
	public String sys_agency_pass_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();
		
		String token = "";
		String id = "";
		String npass = "";
		String npass2 = "";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		if(UtsReg.getIntXl(request.getParameter("id"))){id = request.getParameter("id").trim();}else{jsonObject.put("msg", "id格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}		
		if(UtsReg.getStringPass(request.getParameter("npass"))){npass=request.getParameter("npass").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "新密码 格式错误");return jsonObject.toString();}
		if(UtsReg.getStringPass(request.getParameter("npass2"))){npass2=request.getParameter("npass2").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "重复密码 格式错误");return jsonObject.toString();}
		Administrator administrator=administratorService.selectOne(new EntityWrapper<Administrator>().eq("token", token));
		if(administrator==null){
			jsonObject.put("msg", "请先登陆之后再做修改");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		if(!npass.equals(npass2)){jsonObject.put("error", "1");jsonObject.put("msg", "两次密码不一致");return jsonObject.toString();}
		
		Agency agency = agencyService.selectOne(new EntityWrapper<Agency>().eq("id", id));
		if(agency==null){
			jsonObject.put("error", "1");jsonObject.put("msg", "该用户不存在");return jsonObject.toString();
		}
		
		if(!npass.equals(npass2)){
			jsonObject.put("error", "1");jsonObject.put("msg", "两次密码不一样");return jsonObject.toString();
		}
		
		agency.setLoginpass(MD5Hash.getMd5HashPasswod(npass2));
		
		if(!agencyService.updateById(agency)){
			jsonObject.put("error", "1");jsonObject.put("msg", "失败");return jsonObject.toString();
		}
		
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		return jsonObject.toString();

	}
	
	//代理商头像修改
	@RequestMapping("/sys/agency/touxiang/up")
	@ResponseBody
	public String sys_agency_touxiang_up(HttpServletRequest request) throws MalformedURLException, IOException {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String id = "";
		String img = "";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		if (UtsReg.getIntXl(request.getParameter("id"))){id = request.getParameter("id").trim();}else{jsonObject.put("msg", "id格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getImg(request.getParameter("img"))){img = request.getParameter("img").trim();}else{jsonObject.put("msg", "图片格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Administrator administrator=administratorService.selectOne(new EntityWrapper<Administrator>().eq("token", token));
		if(administrator==null){
			jsonObject.put("msg", "请先登陆之后再做修改");jsonObject.put("error", "1");return jsonObject.toString();
		}
		Agency agency =agencyService.selectOne(new EntityWrapper<Agency>().eq("id", id));
		if(agency==null){
			jsonObject.put("msg", "该代理商不存在");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		agency.setImg(img);
		if(!agencyService.updateById(agency)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//代理商性别修改
	@RequestMapping("/sys/agency/sex/up")
	@ResponseBody
	public String sys_agency_sex_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String id = "";
		String sex = "";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		if (UtsReg.getIntXl(request.getParameter("id"))){id = request.getParameter("id").trim();}else{jsonObject.put("msg", "id格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getSex(request.getParameter("sex"))){sex = request.getParameter("sex").trim();}else{jsonObject.put("msg", "性别格式错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Administrator administrator=administratorService.selectOne(new EntityWrapper<Administrator>().eq("token", token));
		if(administrator==null){
			jsonObject.put("msg", "请先登陆之后再做修改");jsonObject.put("error", "1");return jsonObject.toString();
		}
		Agency agency =agencyService.selectOne(new EntityWrapper<Agency>().eq("id", id));
		if(agency==null){
			jsonObject.put("msg", "该代理商不存在");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		if ("男".equals(sex)) {
			agency.setSex(sex);
		}else if("女".equals(sex)){
			agency.setSex(sex);
		}else {
			jsonObject.put("msg", "只能输入男/女");jsonObject.put("error", "1");return jsonObject.toString();
		}
		if(!agencyService.updateById(agency)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//代理商年龄修改
	@RequestMapping("/sys/agency/age/up")
	@ResponseBody
	public String sys_agency_age_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String id = "";
		String age = "";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		if (UtsReg.getIntXl(request.getParameter("id"))){id = request.getParameter("id").trim();}else{jsonObject.put("msg", "id格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getIntXl(request.getParameter("age"))){age = request.getParameter("age").trim();}else{jsonObject.put("msg", "年龄格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Administrator administrator=administratorService.selectOne(new EntityWrapper<Administrator>().eq("token", token));
		if(administrator==null){
			jsonObject.put("msg", "请先登陆之后再做修改");jsonObject.put("error", "1");return jsonObject.toString();
		}
		Agency agency =agencyService.selectOne(new EntityWrapper<Agency>().eq("id", id));
		if(agency==null){
			jsonObject.put("msg", "该代理商不存在");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		agency.setAge(age);
		if(!agencyService.updateById(agency)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}

	//代理商昵称修改
	@RequestMapping("/sys/agency/nicheng/up")
	@ResponseBody
	public String sys_agency_nicheng_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String id = "";
		String nicheng = "";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		if (UtsReg.getIntXl(request.getParameter("id"))){id = request.getParameter("id").trim();}else{jsonObject.put("msg", "id格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringKB(request.getParameter("nicheng"))){nicheng = request.getParameter("nicheng").trim();}else{jsonObject.put("msg", "昵称格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Administrator administrator=administratorService.selectOne(new EntityWrapper<Administrator>().eq("token", token));
		if(administrator==null){
			jsonObject.put("msg", "请先登陆之后再做修改");jsonObject.put("error", "1");return jsonObject.toString();
		}
		Agency agency =agencyService.selectOne(new EntityWrapper<Agency>().eq("id", id));
		if(agency==null){
			jsonObject.put("msg", "该代理商不存在");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		agency.setNicheng(nicheng);
		if(!agencyService.updateById(agency)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//代理商名字修改
	@RequestMapping("/sys/agency/name/up")
	@ResponseBody
	public String sys_agency_name_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String id = "";
		String name = "";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		if (UtsReg.getIntXl(request.getParameter("id"))){id = request.getParameter("id").trim();}else{jsonObject.put("msg", "id格式错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringZname(request.getParameter("name"))){name = request.getParameter("name").trim();}else{jsonObject.put("msg", "名字格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Administrator administrator=administratorService.selectOne(new EntityWrapper<Administrator>().eq("token", token));
		if(administrator==null){
			jsonObject.put("msg", "请先登陆之后再做修改");jsonObject.put("error", "1");return jsonObject.toString();
		}
		Agency agency =agencyService.selectOne(new EntityWrapper<Agency>().eq("id", id));
		if(agency==null){
			jsonObject.put("msg", "该代理商不存在");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		agency.setName(name);
		if(!agencyService.updateById(agency)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//代理商邮箱修改
	@RequestMapping("/sys/agency/email/up")
	@ResponseBody
	public String sys_agency_email_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String id = "";
		String email = "";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		if (UtsReg.getIntXl(request.getParameter("id"))){id = request.getParameter("id").trim();}else{jsonObject.put("msg", "id格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringEmail(request.getParameter("email"))){email = request.getParameter("email").trim();}else{jsonObject.put("msg", "邮箱格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Administrator administrator=administratorService.selectOne(new EntityWrapper<Administrator>().eq("token", token));
		if(administrator==null){
			jsonObject.put("msg", "请先登陆之后再做修改");jsonObject.put("error", "1");return jsonObject.toString();
		}
		Agency agency =agencyService.selectOne(new EntityWrapper<Agency>().eq("id", id));
		if(agency==null){
			jsonObject.put("msg", "该代理商不存在");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		agency.setEmail(email);
		if(!agencyService.updateById(agency)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//删除代理商
	@RequestMapping(value={"/sys/agency/delbyuid"})
	@ResponseBody
	public String sys_agency_delbyuid(HttpServletRequest request){
		JSONObject jsonObject = new JSONObject(); 
		
		String token="";
		String id="";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		if(UtsReg.getIntXl(request.getParameter("id"))){id=request.getParameter("id").trim();}else{jsonObject.put("msg", "id格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Administrator administrator=administratorService.selectOne(new EntityWrapper<Administrator>().eq("token", token));
		if(administrator==null){
			jsonObject.put("msg", "请先登陆之后再做修改");jsonObject.put("error", "1");return jsonObject.toString();
		}
		if(agencyService.selectOne(new EntityWrapper<Agency>().eq("id", id))==null){
			jsonObject.put("msg", "该代理商不存在");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		if(!agencyService.deleteById(id)){
			jsonObject.put("msg", "失败");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		jsonObject.put("msg", "成功");
		jsonObject.put("error", "0");
		return jsonObject.toString();
	}
	
	//管理员查询所有代理商
	@RequestMapping("/sys/agency/list")
	@ResponseBody
	public String agency_user_list(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String loginname = "";
		String name = "";
		String nicheng = "";
		String img = "";
		String sex = "";
		String age = "";
		String email = "";
		String page = "";
		String size = "";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		if(UtsReg.getIsPhone(request.getParameter("loginname"))){loginname = request.getParameter("loginname").trim();}else{jsonObject.put("msg", "登录名格式错误");jsonObject.put("error", "1");}	
		if(UtsReg.getStringZname(request.getParameter("name"))){name = request.getParameter("name").trim();}else{jsonObject.put("msg", "名字格式错误");jsonObject.put("error", "1");}	
		if(UtsReg.getStringKB(request.getParameter("nicheng"))){nicheng = request.getParameter("nicheng").trim();}else{jsonObject.put("msg", "昵称格式错误");jsonObject.put("error", "1");}	
		if(UtsReg.getImg(request.getParameter("img"))){img = request.getParameter("img").trim();}else{jsonObject.put("msg", "头像格式错误");jsonObject.put("error", "1");}	
		if(UtsReg.getSex(request.getParameter("sex"))){sex = request.getParameter("sex").trim();}else{jsonObject.put("msg", "性别格式错误");jsonObject.put("error", "1");}	
		if(UtsReg.getIntXl(request.getParameter("age"))){age = request.getParameter("age").trim();}else{jsonObject.put("msg", "年龄格式错误");jsonObject.put("error", "1");}	
		if(UtsReg.getStringEmail(request.getParameter("email"))){email = request.getParameter("email").trim();}else{jsonObject.put("msg", "邮箱格式错误");jsonObject.put("error", "1");}	
		if(UtsReg.getStringKB(request.getParameter("page"))){page=request.getParameter("page").trim();}else{page = "1";}
		if(UtsReg.getStringKB(request.getParameter("size"))){size=request.getParameter("size").trim();}else{size = "10";}
		
		Administrator administrator=administratorService.selectOne(new EntityWrapper<Administrator>().eq("token", token));
		if(administrator==null){
			jsonObject.put("msg", "请重新登陆");jsonObject.put("error", "1");return jsonObject.toString();
		}
		PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(size));	
		
		List<Agency> list = null;
		EntityWrapper<Agency> ew=new EntityWrapper<Agency>();
		if(loginname.length()>0){ew.like("loginname", loginname);}
		if(name.length()>0){ew.like("name", name);}
		if(nicheng.length()>0){ew.like("nicheng", nicheng);}
		if(img.length()>0){ew.like("img", img);}
		if(sex.length()>0){ew.like("sex", sex);}
		if(age.length()>0){ew.like("age", age);}
		if(email.length()>0){ew.like("email", email);}
		list = agencyService.selectList(ew);
		
		if (list==null) {jsonObject.put("msg", "没有数据");jsonObject.put("error", "1");return jsonObject.toString();}
		PageInfo<Agency> pages = new PageInfo<Agency>(list);

		//过滤重要信息
		for(int i = 0;i<pages.getList().size();i++){
			Agency agency = pages.getList().get(i);
			agency.setToken("");
			agency.setLoginpass("");
		}
		jsonObject.put("data", pages);
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
		
	//管理员根据id查询代理商
	@RequestMapping("/sys/agency/findbyid")
	@ResponseBody
	public String sys_agency_findbyid(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String id = "";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		if(UtsReg.getIntXl(request.getParameter("id"))){id = request.getParameter("id").trim();}else{jsonObject.put("msg", "id格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		Administrator administrator=administratorService.selectOne(new EntityWrapper<Administrator>().eq("token", token));
		if(administrator==null){
			jsonObject.put("msg", "请重新登陆");jsonObject.put("error", "1");return jsonObject.toString();
		}
		Agency agency =agencyService.selectOne(new EntityWrapper<Agency>().eq("id", id));
		if(agency==null){
			jsonObject.put("msg", "该代理商信息不存在");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		agency.setToken("");
		agency.setLoginpass("");
		jsonObject.put("data", agency);
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//管理员添加代理商
	@RequestMapping(value={"/sys/agency/add"})
	@ResponseBody
	public String sys_agency_add(HttpServletRequest request){
		JSONObject jsonObject = new JSONObject(); 
		
		String token="";
		String loginname="";
		String loginpass="";
		String name="";
		String phone="";
		String img="";
		String sex="";
		String age="";
		String address="";
		String email="";
		if(UtsReg.getStringKB(request.getParameter("token"))){token=request.getParameter("token").trim();}else{jsonObject.put("msg", "token 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if(UtsReg.getIsPhone(request.getParameter("loginname"))){loginname=request.getParameter("loginname").trim();}else{jsonObject.put("msg", "登录名格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if(UtsReg.getStringPass(request.getParameter("loginpass"))){loginpass=request.getParameter("loginpass").trim();}else{jsonObject.put("msg", "密码格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if(UtsReg.getStringZname(request.getParameter("name"))){name=request.getParameter("name").trim();}else{jsonObject.put("msg", "名字格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if(UtsReg.getIsPhone(request.getParameter("phone"))){phone=request.getParameter("phone").trim();}else{jsonObject.put("msg", "电话格式 错误");jsonObject.put("error", "1");}
		if(UtsReg.getImg(request.getParameter("img"))){img=request.getParameter("img").trim();}else{jsonObject.put("msg", "头像格式 错误");jsonObject.put("error", "1");}
		if(UtsReg.getSex(request.getParameter("sex"))){sex=request.getParameter("sex").trim();}else{jsonObject.put("msg", "性别格式 错误");jsonObject.put("error", "1");}
		if(UtsReg.getIntXl(request.getParameter("age"))){age=request.getParameter("age").trim();}else{jsonObject.put("msg", "年龄格式 错误");jsonObject.put("error", "1");}
		if(UtsReg.getStringKB(request.getParameter("address"))){address=request.getParameter("address").trim();}else{jsonObject.put("msg", "地址格式 错误");jsonObject.put("error", "1");}
		if(UtsReg.getStringEmail(request.getParameter("email"))){email=request.getParameter("email").trim();}else{jsonObject.put("msg", "邮箱格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Administrator administrator =administratorService.selectOne(new EntityWrapper<Administrator>().eq("token", token));
		if(administrator==null){jsonObject.put("msg", "请重新登录");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Agency agency = new Agency();
		
		if (agencyService.selectCount(new EntityWrapper<Agency>().eq("loginname", loginname))>0) {jsonObject.put("msg", "该代理商已存在,不可重复添加");jsonObject.put("error", "1");return jsonObject.toString();}
		if (agencyService.selectCount(new EntityWrapper<Agency>().eq("email", email))>0) {jsonObject.put("msg", "该代理商邮箱已存在,不可重复添加");jsonObject.put("error", "1");return jsonObject.toString();}
		
		agency.setLoginname(loginname);
		agency.setLoginpass(MD5Hash.getMd5HashPasswod(loginpass));
		agency.setName(name);
		agency.setPhone(phone);
		agency.setImg(img);
		if ("男".equals(sex)) {
			agency.setSex(sex);
		}else if("女".equals(sex)){
			agency.setSex(sex);
		}else {
			jsonObject.put("msg", "只能输入男/女");jsonObject.put("error", "1");return jsonObject.toString();
		}
		agency.setAge(age);
		agency.setAddress(address);
		agency.setEmail(email);
		if(!agencyService.insert(agency)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();
	}
	
	//用户资料修改
	@RequestMapping("/sys/agency/ziliao/up")
	@ResponseBody
	public String sys_user_ziliao_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String id = "";
		String name = "";
		String sex = "";
		String age = "";
		String img = "";
		String email = "";
		String nicheng = "";
		if (UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getIntXl(request.getParameter("id"))){id = request.getParameter("id").trim();}else{jsonObject.put("msg", "id 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringZname(request.getParameter("name"))){name = request.getParameter("name").trim();}else{jsonObject.put("msg", "名字格式 错误");jsonObject.put("error", "1");}
		if (UtsReg.getSex(request.getParameter("sex"))){sex = request.getParameter("sex").trim();}else{jsonObject.put("msg", "性别格式 错误");jsonObject.put("error", "1");}
		if (UtsReg.getIntXl(request.getParameter("age"))){age = request.getParameter("age").trim();}else{jsonObject.put("msg", "年龄格式 错误");jsonObject.put("error", "1");}
		if (UtsReg.getImg(request.getParameter("img"))){img = request.getParameter("img").trim();}else{jsonObject.put("msg", "头像格式 错误");jsonObject.put("error", "1");}
		if (UtsReg.getStringEmail(request.getParameter("email"))){email = request.getParameter("email").trim();}else{jsonObject.put("msg", "邮箱格式 错误");jsonObject.put("error", "1");}
		if (UtsReg.getStringKB(request.getParameter("nicheng"))){nicheng = request.getParameter("nicheng").trim();}else{jsonObject.put("msg", "昵称格式 错误");jsonObject.put("error", "1");}
		
		Administrator administrator =administratorService.selectOne(new EntityWrapper<Administrator>().eq("token", token));
		if(administrator==null){
			jsonObject.put("msg", "请重新登录");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		Agency agency = agencyService.selectOne(new EntityWrapper<Agency>().eq("id", id));
		if(agency==null){
			jsonObject.put("msg", "该用户不存在");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
			agency.setName(name);
			if ("男".equals(sex)) {
				agency.setSex(sex);
			}else if("女".equals(sex)){
				agency.setSex(sex);
			}else {
				jsonObject.put("msg", "只能输入男/女");jsonObject.put("error", "1");return jsonObject.toString();
			}
			agency.setNicheng(nicheng);
			agency.setAge(age);
			agency.setImg(img);
			agency.setEmail(email);
		
		if(!agencyService.updateById(agency)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	
}
